package xps.and.uudaijia.userclient.presenter;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.ViewHolder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import xps.and.uudaijia.userclient.App;
import xps.and.uudaijia.userclient.EventBusCommond.EventBusRequestReserveTime;
import xps.and.uudaijia.userclient.R;
import xps.and.uudaijia.userclient.view.MainActivity;
import xps.and.uudaijia.userclient.view.custom.WheelView;

public class MainPresenter extends BasePresenter<MainActivity> {
    private String TAG = getClass().getSimpleName();
    private App application = null;
    private String REQUEST_RESERVETIME = "代驾";//标记来自代驾fragmengt还是拼车Fragment

    @Override
    protected void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        application = App.getInstance();
    }

    public void chooseReserveTime(Activity ac, String from) {
        REQUEST_RESERVETIME = from;
        DialogPlus dialog = null;
        ViewHolder contentViewHolder = new ViewHolder(ac.getLayoutInflater().inflate(R.layout.reserve_time, null));
        ViewHolder headerViewHolder = new ViewHolder(ac.getLayoutInflater().inflate(R.layout.header, null));

        View contentView = contentViewHolder.getInflatedView();
        WheelView wv_wv_date = (WheelView) contentView.findViewById(R.id.wv_date);
        WheelView wv_wv_hour = (WheelView) contentView.findViewById(R.id.wv_hour);
        WheelView wv_wv_minute = (WheelView) contentView.findViewById(R.id.wv_minute);

        wv_wv_date.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                String Date;
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Date date = new Date();
                if ("代驾".equals(REQUEST_RESERVETIME)) {
                    EventBusRequestReserveTime eventBusRequestReserveTime = new EventBusRequestReserveTime();
                    eventBusRequestReserveTime.setFrom(EventBusRequestReserveTime.FROM_DAIJIA);
                    Date = formatter.format(date);
                    if (item.equals("今天")) {
                        Date = formatter.format(date);
                    } else if (item.equals("明天")) {
                        Date = formatter.format(getNextDate(date));
                    } else if (item.equals("后天")) {
                        Date = formatter.format(getTewDate(date));
                    }
                    org.greenrobot.eventbus.EventBus.getDefault().post(eventBusRequestReserveTime);
                    try {
                        eventBusRequestReserveTime.setDate(formatter.parse(Date));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                } else if ("拼车".equals(REQUEST_RESERVETIME)) {
                    EventBusRequestReserveTime eventBusRequestReserveTime = new EventBusRequestReserveTime();
                    eventBusRequestReserveTime.setFrom(EventBusRequestReserveTime.FROM_PINCHE);
                    org.greenrobot.eventbus.EventBus.getDefault().post(eventBusRequestReserveTime);
                }
            }
        });


        wv_wv_date.setItems(Arrays.asList("今天", "明天", "后天"));
        wv_wv_hour.setItems(getHoursList());
        wv_wv_minute.setItems(getMinutsList());

        View headerView = headerViewHolder.getInflatedView();
        ((TextView) headerView.findViewById(R.id.tv_hint)).setText("预约时间:");

        DialogPlus finalDialog = dialog;
        headerView.findViewById(R.id.tv_subComfirm).setOnClickListener((v) -> {
                    finalDialog.dismiss();

                }
        );

        dialog = DialogPlus.newDialog(ac)
                .setContentHolder(contentViewHolder)
                .setGravity(Gravity.BOTTOM)
                .setHeader(headerView)
                //.setOnClickListener(this)
                .setInAnimation(R.anim.dialog_in_anim)
                .setOutAnimation(R.anim.dialog_out_anim_bottom)
                .create();
        dialog.show();
    }

    ArrayList<String> getHoursList() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i <= 23; i++) {
            list.add(i + "时");

        }
        return list;
    }

    ArrayList<String> getMinutsList() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 10; i <= 60; i += 10) {
            list.add(i + "");
        }
        return list;
    }

    private static Date getNextDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +1); //今天的时间加一天
        date = calendar.getTime();
        return date;
    }

    private static Date getTewDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +2); //今天的时间加一天
        date = calendar.getTime();
        return date;
    }
}
